<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/global.css?t=1515376178738-1" media="all">
    <script src="//wx.hfjy.com/gongkai/Public/static/jquery-2.0.3.min.js"></script>
    <script src="layui/layui.js"></script>
    <style>
        .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
    </style>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>常规使用：普通图片上传</legend>
</fieldset>

<div class="layui-upload">
    <button type="button" class="layui-btn" id="test1">上传图片</button>
    <div class="layui-upload-list">
        <div style="margin-bottom: 30px;">
            <img class="layui-upload-img pic1" id="pic1">
            <p id="pic-text" style="width: 92px;text-align: center;cursor:pointer;display: none;">删除图片</p>
        </div>
    </div>
</div>

<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var tmp_upload_pic='/upload-pic/images/default-upload.png'
        document.querySelector('.pic1' ).setAttribute('src',tmp_upload_pic)
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/upload-pic/upload.php'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                console.log(res.data.src)
                document.querySelector('.pic1' ).setAttribute('src',res.data.src)
                $('#pic-text' ).show()
                $('#pic-text' ).click(function(){
                    document.querySelector('.pic1' ).setAttribute('src',tmp_upload_pic)
                    $(this ).hide()
                })
                //$('#pic1' ).attr('src',res.data.src)
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //多图片上传
        upload.render({
            elem: '#test2'
            ,url: '/upload/'
            ,multiple: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            ,done: function(res){
                //上传完毕
            }
        });
    });
</script>
</body>
</html>
